﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Manager;
using System.Data.SqlClient;
using System.Collections;

namespace ClassLibrary1
{
    public class DichVu
    {
        private DbManager db;

        public DichVu() {
            db = new DbManager();
        }
        
        public DataTable getTopOneCategory() {
            DataTable tableIdCategory = getAllCategory();
            DataTable resultTopOne = new DataTable();
            Boolean clone = false;
            for (int i = 0; i < tableIdCategory.Rows.Count; i++) {
                DataTable tableCurent = new DataTable();
                SqlParameter paramA = new SqlParameter("a", tableIdCategory.Rows[i]["CategoryID"].ToString());
                tableCurent = db.getData("select top 1 C.CategoryName, C.Link, Images, NewsName, NewsSummary from News N inner join Categories C on N.IDCategory = C.CategoryID where IDCategory=@a order by NewsID desc", paramA);
                if (!clone) {
                    resultTopOne = tableCurent.Clone();
                    clone = true;
                }
                foreach (DataRow row in tableCurent.Rows) {
                    resultTopOne.ImportRow(row);
                }
            }
           
            //resultTopOne.Rows.Add(list[0]);
            return resultTopOne;
        }

        public DataTable getKNLV() {
            DataTable resultKNLV = new DataTable();
            resultKNLV = db.getData("select top 1 C.CategoryName, C.Link, N.Images, N.NewsName, N.NewsSummary from News N inner join Categories C on N.IDCategory = C.CategoryID where C.Type = '1' order by NewsID desc");
            return resultKNLV;
        }

        public DataTable getDTCT() {
            DataTable resultDTCT = new DataTable();
            resultDTCT = db.getData("select top 10 Location from Images order by ImageID desc");
            return resultDTCT;
        }

        private DataTable getAllCategory() {
            DataTable dt = null;
            dt = db.getData("select CategoryID from Categories where Parrent is not null");
            return dt;
        }
    }
}
